<?php
/**
 * @dexription : get the parameters from webpage and intergration them to gearmand queue.
 * @author     : stephen.mo
 * @date       : Nov 4,2010
 */
header("Access-Control-Allow-Headers: x-requested-with");
header('Access-Control-Allow-Origin: *');
header('Content-type: text/json');
//ini_set('display_errors','on');
require_once 'TVB/MSApi/Client/requires.php';
require_once 'TVB/Logger/TVBLog.php';
TVBLog::config("/data/logger/conf/config.xml");

error_log ( var_export ( $_REQUEST, true ));
 
$type = (!isset($_REQUEST['logtype']) || empty($_REQUEST['logtype'])) ? null : $_REQUEST['logtype'];
if (empty($type)) {
    echo -1;
    die();
}

$access_datetime = (!isset($_REQUEST['access_datetime']) || empty($_REQUEST['access_datetime'])) ? date('Y-m-d H:i:s',date('U',strtotime(date('r')))) : date('Y-m-d H:i:s',$_REQUEST['access_datetime']);
$login_id        = (!isset($_REQUEST['login_id']) || empty($_REQUEST['login_id'])) ? 'guest' : $_REQUEST['login_id'];
$client_ip		 = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) &&$_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] :$_SERVER['REMOTE_ADDR'];
$country_code    = (!isset($_REQUEST['country']) || empty($_REQUEST['country'])) ? 'HK' : $_REQUEST['country'];
$url             = (!isset($_REQUEST['url']) || empty($_REQUEST['url'])) ? 'NULL' : $_REQUEST['url'];
$ui_lang         = (!isset($_REQUEST['ui_lang']) || empty($_REQUEST['ui_lang']) || $_REQUEST['ui_lang']=='null') ? 'zh-hant-hk' : $_REQUEST['ui_lang'];
$video_lang      = (!isset($_REQUEST['video_lang']) || empty($_REQUEST['video_lang'])) ? 'NULL' : $_REQUEST['video_lang'];
$video_id        = (!isset($_REQUEST['video_id']) || empty($_REQUEST['video_id'])) ? 'NULL' : $_REQUEST['video_id'];
$video_title     = (!isset($_REQUEST['video_title']) || empty($_REQUEST['video_title'])) ? 'NULL' : $_REQUEST['video_title'];
$programme_id    = (!isset($_REQUEST['programme_id']) || empty($_REQUEST['programme_id'])) ? 'NULL' : $_REQUEST['programme_id'];
$category_id     = (!isset($_REQUEST['category_id']) || empty($_REQUEST['category_id'])) ? 'NULL' : $_REQUEST['category_id'];
$category_path   = (!isset($_REQUEST['category_path']) || empty($_REQUEST['category_path'])) ? 'NULL' : $_REQUEST['category_path'];
$online_datetime = (!isset($_REQUEST['online_datetime']) || empty($_REQUEST['online_datetime'])) ? $access_datetime : date('Y-m-d H:i:s',$_REQUEST['online_datetime']);;
$video_type      = (!isset($_REQUEST['video_type']) || empty($_REQUEST['video_type'])) ? 'NULL' : $_REQUEST['video_type'];
$count_type      = (!isset($_REQUEST['count_type']) || empty($_REQUEST['count_type'])) ? 'NULL' : $_REQUEST['count_type'];
$user_agent      = (!isset($_REQUEST['user_agent']) || empty($_REQUEST['user_agent'])) ? 'NULL' : $_REQUEST['user_agent'];
$page_type       = (!isset($_REQUEST['page_type']) || empty($_REQUEST['page_type'])) ? 'NULL' : $_REQUEST['page_type'];
$step            = (!isset($_REQUEST['step']) || empty($_REQUEST['step'])) ? 'NULL' : $_REQUEST['step'];
$unique_id       = (!isset($_REQUEST['unique_id']) || empty($_REQUEST['unique_id'])) ? 'NULL' : $_REQUEST['unique_id'];

switch($type) {
    case 'fp_video_access':
        if (strtolower($login_id)!='guest') {
            $obj = new MSApiClient();
            $requestData['cookie'] = base64_encode(urldecode($login_id));
            $result = $obj->restRequest('Member','checkLoginByCookie',$requestData);
            $login_id = (empty($result->result->login_id)) ? 'guest' : (string)$result->result->login_id;

        }
        $log_entity = array('action'=>'2','level'=>'H','style'=>'B',
                            'data'=>array('type'=>$type,
                                          'datetime'=>$access_datetime,
                                          'member_id'=>$login_id,
                                          'ui_lang'=>$ui_lang,
                                          'client_ip'=>$client_ip,
                                          'country_code'=>$country_code,
                                          'url'=>$url,
                                          'video_id'=>$video_id,
                                          'video_title'=>$video_title,
                                          'video_lang'=>$video_lang,
                                          'programme_id'=>$programme_id,
                                          'category_id'=>$category_id,
                                          'category_path'=>$category_path,
                                          'online_datetime'=>$online_datetime,
                                          'video_type'=>$video_type,
                                          'count_type'=>$count_type,
                                          'user_agent'=>$user_agent)
                         );
            break;
    case 'fp_page_access':
        if (strtolower($login_id)!='guest') {
            $obj = new MSApiClient();
            $requestData['cookie'] = base64_encode(urldecode($login_id));
            $result = $obj->restRequest('Member','checkLoginByCookie',$requestData);
            $login_id = (empty($result->result->login_id)) ? 'guest' : (string)$result->result->login_id;
        }
        $log_entity = array('action'=>'2','level'=>'H','style'=>'B',
                            'data'=>array('type'=>$type,
                                          'datetime'=>$access_datetime,
                                          'member_id'=>$login_id,
                                          'ui_lang'=>$ui_lang,
                                          'client_ip'=>$client_ip,
                                          'country_code'=>$country_code,
                                          'url'=>$url,
                                          'page_type'=>$page_type,
                                          'step'=>$step,
                                          'unique_id'=>$unique_id)
                         );
        break;
    default:
	    echo "Not support this type log!";
	    die();
}

//write log to job server.
$message = json_encode($log_entity);
TVBLog::$Log['mytvplus']->info($message);

error_log ( 'finishh' ) ;
echo json_encode(array('result', 'success'));
?>
